<template>
  <div>
    <!-- // 面包屑 -->
    <MBX></MBX>
    <el-card class="xia">
      <el-alert
        title="添加商品信息"
        :closable="false"
        type="info"
        center
        show-icon
      >
      </el-alert>
      <!-- // 步骤条 -->
      <el-steps
        :active="active * 1"
        finish-status="success"
        class="xia"
        align-center
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品删除"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form :data="from">
        <!-- // 左侧标签页 -->
        <el-tabs
          v-model="active"
          @tab-click="handleClick"
          tab-position="left"
          class="xia"
          :before-leave="beforeLeave"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称">
              <el-input v-model="from.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
              <el-input v-model="from.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量">
              <el-input v-model="from.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量">
              <el-input v-model="from.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              {{ check_id }}
              <el-cascader
                :props="defaltProps"
                :options="cateData"
                v-model="check_id"
                @change="handleChange"
              >
              </el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <!-- 渲染动态 -->
            <el-form-item
              v-for="item1 in manyDong"
              :key="item1.id"
              :label="item1.attr_name"
            >
              <!-- // 复选框 -->
              <el-checkbox-group v-model="item1.attr_vals">
                <el-checkbox
                  :label="item2"
                  v-for="(item2, index) in item1.attr_vals"
                  :key="index"
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              :label="item3.attr_name"
              v-for="item3 in manyJing"
              :key="item3.id"
            >
              <el-input v-model="item3.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <!-- 图片 -->
            <el-upload
              class="upload-demo"
              action="http://www.ysqorz.top:8888/api/private/v1/"
              :headers="headers"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
              :on-success="chenggong"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <!-- // 富文本 -->
            <quill-editor v-model="from.goods_introduce"> </quill-editor>
            <el-button type="primary" class="xia" @click="AlladdShop"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: '',
  components: {},
  data() {
    return {
      // 设置请求头
      headers: {
        Authorization: sessionStorage.getItem('token')
      },
      cateData: [],
      from: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_cat: '',
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      check_id: [], // 分类级别id
      defaltProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        children: 'children',
        value: 'cat_id'
      }, // 默认设置
      active: 0, // 当前激活的步骤
      manyDong: [],
      manyJing: []
    }
  },
  created() {
    this.addShop()
  },
  mounted() {},
  computed: {
    // dang_check_id() {
    //   return this.check_id[this.check_id.length - 1]
    // }
  },
  methods: {
    // 添加商品
    async AlladdShop() {
      console.log('动态:', this.manyDong)
      console.log('静态:', this.manyJing)
      // 赋值 分割为字符串
      this.from.goods_cat = this.check_id.join(',')
      // 动态数据处理
      const dtData = this.manyDong.map((item) => {
        console.log(item)
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals.join(',')
        }
      })
      // 静态态数据处理
      const jtData = this.manyJing.map((item) => {
        console.log(item)
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals
        }
      })
      const hb = [...dtData, ...jtData]

      this.from.attrs = hb
      console.log(this.from)
      const res = await this.$QHL.addShopAll(this.from)

      this.addShop()
      this.$router.push('/goods')
      console.log(res)
      // console.log(this.from)
    },
    // 触发预览效果
    handlePreview() {},
    // 上传成功
    chenggong(res) {
      console.log('上传成功:', res)
      // console.log(this.from.pics)
      this.from.push({ pics: res.data.tmp_path })
    },
    //   上传删除
    handleRemove(res) {
      console.log('删除成功:', res)
      const index = this.from.pic.findIndex((item) => {
        return item.pic === res.response.data.tmp_path
      })
      this.from.pics.splice(index, 1)
    },
    beforeLeave(activeName, oldActiveName) {
      //   console.log(activeName, oldActiveName)
      if (oldActiveName === '0' && this.check_id.length !== 3) {
        this.$message({
          type: 'error',
          message: '请先选择商品分类'
        })
        return false
      }
    },
    async addShop() {
      const res = await this.$QHL.ShopCateListapi()
      // console.log(res)
      this.cateData = res
    },
    async handleClick() {
      // 被选中时触发的
      if (this.active === '1') {
        const res = await this.$QHL.ShuShopCateApi(
          this.check_id[this.check_id.length - 1]
        )
        console.log(res)
        res.forEach((item) => {
          //   console.log(item)
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(',') : []
        })
        this.manyDong = res
      } else if (this.active === '2') {
        // 调动态
        this.manyJing = await this.$QHL.ShuShopCateApi(this.check_id[2], 'only')
        console.log(this.manyJing)
        // 调静态
      }
      //   console.log(this.activeName)
      //   console.log(this.active)
    },
    handleChange() {}
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-step__title {
  font-size: 12px;
}
::v-deep .ql-editor {
  height: 300px;
}
</style>
